package com.runvp.common.pay.adapay.demo;

import com.alibaba.fastjson.JSON;
import com.huifu.adapay.model.refund.Refund;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jane.zhao
 */
public class RefundDemo extends BaseDemo{

    /**
     * 运行退款类接口
     * @throws Exception 异常
     */
    public static void executeRefundTest(String paymentId) throws Exception{
        RefundDemo demo = new RefundDemo();
        //退款接口
        Refund refund = demo.executeRefund(paymentId);
        //退款查询接口（通过pamentId查询）
        demo.queryByPaymentId(paymentId);
        //退款查询接口（通过refundId查询）
        demo.queryByRefundId(refund.getId());
    }

    /**
     * 执行一个退款交易
     * @param paymentId 要退款的原支付paymentId
     * @return 创建的退款对象
     * @throws Exception 异常
     */
    public Refund executeRefund(String paymentId) throws Exception {
        System.out.println("=======execute refund begin=======");
        Map<String, Object> refundParams = new  HashMap<String, Object>(2);
        refundParams.put("refund_amt", "0.01");
        refundParams.put("app_id", "app_7d87c043-aae3-4357-9b2c-269349a980d6");
        refundParams.put("refund_order_no", "jsdk_refund_"+System.currentTimeMillis());
        Refund refund = Refund.create(paymentId, refundParams);
        System.out.println("refund result="+JSON.toJSONString(refund));
        return refund;
    }

    /**
     * 根据原支付id查询一个退款交易
     * @param paymentId 要查询退款的原支付paymentId
     * @return 查询的退款对象，可能含多个退款明细RefundDetail
     * @throws Exception 异常
     */
    public Refund queryByPaymentId(String paymentId) throws Exception{
        System.out.println("=======query refund by paymentId begin=======");
        Map<String, Object> chargeParams = new  HashMap<String, Object>(1);
        chargeParams.put("payment_id", paymentId);
        Refund refund = Refund.query(chargeParams);
        System.out.println("query refund by paymentid result="+JSON.toJSONString(refund));
        return refund;
    }

    /**
     * 根据退款refundId查询一个退款交易
     * @param refundId 要查询的退款refundId
     * @return 查询的退款对象
     * @throws Exception 异常
     */
    public Refund queryByRefundId(String refundId) throws Exception{
        System.out.println("=======query refund by refundid begin=======");
        Map<String, Object> chargeParams = new  HashMap<String, Object>(1);
        chargeParams.put("refund_id", refundId);
        Refund refund = Refund.query(chargeParams);
        System.out.println("query refund by refundid result="+JSON.toJSONString(refund));
        return refund;
    }
}
